package com.best.oasis.settlement.view.menuItem
{


	
	import com.best.oasis.settlement.common.vo.system.MenuItemVO;
	
	import mx.collections.ArrayCollection;
	import mx.collections.Sort;
	import mx.collections.SortField;
	import mx.controls.AdvancedDataGrid;
	import mx.controls.Alert;
	import mx.controls.advancedDataGridClasses.AdvancedDataGridGroupItemRenderer;
	import mx.core.IUIComponent;
	import mx.effects.Zoom;
	import mx.events.DragEvent;
	import mx.managers.DragManager;
	
	public class MyAdvancedDataGridGroupItemRenderer extends AdvancedDataGridGroupItemRenderer {
		public var children:ArrayCollection;
		public var zoom:Zoom;
		public function MyAdvancedDataGridGroupItemRenderer() {
			super();
			this.addEventListener(DragEvent.DRAG_ENTER,grid_dragEnterHandler);
			this.addEventListener(DragEvent.DRAG_DROP,grid_dragDropHandler);
			this.addEventListener(DragEvent.DRAG_OVER, grid_dragOverHandler);
			this.addEventListener(DragEvent.DRAG_EXIT, grid_dragExitHandler);
		}
		protected function grid_dragEnterHandler(event:DragEvent):void {
			var obj:IUIComponent = IUIComponent(event.currentTarget);
			DragManager.acceptDragDrop(obj);
		}
		/**
		 * 当鼠标移过时，将字体颜色更改
		 */
		protected function grid_dragOverHandler(event:DragEvent):void{
			this.setStyle("color",'#00ffff');
		}
		/**
		 * 当鼠标移出时，将字体颜色还原
		 */
		protected function grid_dragExitHandler(event:DragEvent):void{
			this.setStyle("color",'black');
		}
		/**
		 * 当鼠标松开时执行的事件
		 * 把被移动的节点的父节点设置为目标节点，重新构建树
		 * 目标节点不能使被移动节点的子孙节点
		 * 将字体颜色还原
		 */
		protected function grid_dragDropHandler(event:DragEvent):void
		{
			//目标vo
			var target:MenuItemVO = new MenuItemVO();
			target = (event.currentTarget as MyAdvancedDataGridGroupItemRenderer).data as MenuItemVO;
			//MenuChangeManageView界面的list
			var list:ArrayCollection = (((this.parent.parent.parent) as AdvancedDataGrid).parent.parent as MenuChangeManageView).list;
			//被移动的vo
			var origin:MenuItemVO = ((this.parent.parent.parent) as AdvancedDataGrid).selectedItem as MenuItemVO;
			
			if(checkIsParent(target,origin,list)){
				Alert.show("不能将该节点移至他的子孙节点。");
				return;
			}
			
			if(target.id != origin.id){
				changeOriginParent(target,origin,list);
			} 
			
			setSource(list);
			
			this.setStyle("color",'black');
			
		}
		/**
		 * 判断target在list中，是否是origin的子孙节点
		 */
		private function checkIsParent(target:MenuItemVO,origin:MenuItemVO,list:ArrayCollection):Boolean{
			var treeList:ArrayCollection = getTree(list);
			children = new ArrayCollection();
			for each(var item:MenuItemVO in treeList){
				if(item.id != origin.id){
					var hasSon:MenuItemVO = getItemFromTree(item,origin);
					if(hasSon != null){
						checkHasChild(hasSon);
					}
				} else {
					checkHasChild(item);
				}
			}
			if(children != null && children.length > 0){
				for each(var menu:MenuItemVO in children){
					if(menu.id == target.id){
						return true;
					}
				}
			}
			return false;
		}
		/**
		 * 遍历item，从item的children找到和vo相同id的tVo并返回
		 */
		private function getItemFromTree(item:MenuItemVO, vo:MenuItemVO):MenuItemVO{
			if(item.children != null && item.children.length > 0){
				for each(var tVo:MenuItemVO in item.children){
					if(tVo.id == vo.id){
						return tVo;
					} else {
						getItemFromTree(tVo,vo);
					}
				}
			}
			return null;
		}
		
		/**
		 * 判断该vo下是否有子节点
		 * 		有，则将Vo加入到children
		 */
		private function checkHasChild(vo:MenuItemVO):void{
			if(vo.children != null && vo.children.length > 0){
				for each(var item:MenuItemVO in vo.children){
					children.addItem(item);
					checkHasChild(item);
				}
			}
		}
		
		/**
		 * 若为不同parentid，找出list中和origin相同id的vo,将vo的parent改为target
		 */
		private function changeOriginParent(target:MenuItemVO,origin:MenuItemVO,list:ArrayCollection):void{
			if(list.length > 0){
				for each(var item:MenuItemVO in list){
					if(item.id == origin.id){
						item.parentId = target.id;
						item.parentName = target.name;
					}
				}
			}
		}
		/**
		 * 以list为数据源,构建MenuChangeManageView界面的hd.source
		 */
		private function setSource(result:ArrayCollection):void{
			sortByColName("sortIndex",result);
			
			(((this.parent.parent.parent) as AdvancedDataGrid).parent.parent as MenuChangeManageView).hd.source = null;
			
			var sourceList:ArrayCollection = getTree(result);
			
			(((this.parent.parent.parent) as AdvancedDataGrid).parent.parent as MenuChangeManageView).hd.source = sourceList;
		}
		/**
		 * 将list按照colName排序
		 */
		private function sortByColName(colName:String, list:ArrayCollection):void{
			var sortIndexSortField:SortField = new SortField();
			sortIndexSortField.name = colName;
			sortIndexSortField.numeric = true;
			var numericDataSort:Sort = new Sort();
			numericDataSort.fields = [sortIndexSortField];
			list.sort = numericDataSort;
			list.refresh();
		}
		
		/**
		 * 以list为数据源，构建树
		 */
		private function getTree(result:ArrayCollection):ArrayCollection{
			var sourceList:ArrayCollection = new ArrayCollection();
			if(result.length > 0){
				for each(var item:MenuItemVO in result){
					item.children = null;
				}
				for each(var menu:MenuItemVO in result){
					var parent : MenuItemVO = getParent(menu,result);
					if(parent != null){
						if(parent.children == null){
							parent.children = new ArrayCollection();
						}
						parent.children.addItem(menu);
					} else {
						sourceList.addItem(menu);
					}
				}
			}
			return sourceList;
		}
		/**
		 * 在list中取得vo的parent
		 */
		private function getParent(vo:MenuItemVO,list:ArrayCollection):MenuItemVO{
			for each(var item:MenuItemVO in list){
				if(item.id == vo.parentId){
					return item;
				}
			}
			return null;
		}
	}
}